{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.cnblogs.com/CheeseZH/p/4593349.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python中的random模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python中的random模块用于生成随机数，现对几个random模块中的函数进行简单介绍，如下："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## random.random()\n",
    "用于生成一个0到1的随机浮点数，如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.52607052423701"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "random.random()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## random.uniform(a,b)\n",
    "用于生成一个指定范围内的随机浮点数。两个参数分别是上下限。示例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15.036004844307861"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.uniform(10,20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## random.randint(a,b)\n",
    "用于生成指定范围内的整数。两个参数分别是上下限。示例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(10,20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## random.randrange([start,stop[,step]])\n",
    "从指定范围内，按指定基数递增的规则中获取一个随机数。如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "98"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取10-100区间内的一个偶数随机数\n",
    "random.randrange(10,100,2) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "相当于从[10,12,14,……,96,98]序列中获取一个随机数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## random.choice(sequence)\n",
    "参数sequence表示一个有序类型。  \n",
    "sequence在Python中并不是一种特定的类型，而是泛指一系列类型：list,tuple,字符串都属于sequence。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'dict'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.choice((\"tuple\",\"list\",\"dict\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## random.sample(sequence,k)\n",
    "从指定序列中随机获取指定长度的片段。  \n",
    "sample函数不会修改原有序列，如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 1, 6]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从lists列表中，随机获取3个数\n",
    "lists = [1,2,3,4,5,6,7,8,9]\n",
    "a = random.sample(lists,3) \n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numpy中的random模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在Numpy模块中有random模块，这两个模块在功能和性能方面有一定的差别，  \n",
    "numpy.random模块有以下一些函数和功能模型："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## np.random.randn(d0，d1，……，dn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从标准正态分布中返回一个或多个样本值。  \n",
    "其中(d0,d1,……,dn)为整数型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function randn:\n",
      "\n",
      "randn(...) method of mtrand.RandomState instance\n",
      "    randn(d0, d1, ..., dn)\n",
      "    \n",
      "    Return a sample (or samples) from the \"standard normal\" distribution.\n",
      "    \n",
      "    If positive, int_like or int-convertible arguments are provided,\n",
      "    `randn` generates an array of shape ``(d0, d1, ..., dn)``, filled\n",
      "    with random floats sampled from a univariate \"normal\" (Gaussian)\n",
      "    distribution of mean 0 and variance 1 (if any of the :math:`d_i` are\n",
      "    floats, they are first converted to integers by truncation). A single\n",
      "    float randomly sampled from the distribution is returned if no\n",
      "    argument is provided.\n",
      "    \n",
      "    This is a convenience function.  If you want an interface that takes a\n",
      "    tuple as the first argument, use `numpy.random.standard_normal` instead.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    d0, d1, ..., dn : int, optional\n",
      "        The dimensions of the returned array, should be all positive.\n",
      "        If no argument is given a single Python float is returned.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    Z : ndarray or float\n",
      "        A ``(d0, d1, ..., dn)``-shaped array of floating-point samples from\n",
      "        the standard normal distribution, or a single such float if\n",
      "        no parameters were supplied.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    random.standard_normal : Similar, but takes a tuple as its argument.\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    For random samples from :math:`N(\\mu, \\sigma^2)`, use:\n",
      "    \n",
      "    ``sigma * np.random.randn(...) + mu``\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.random.randn()\n",
      "    2.1923875335537315 #random\n",
      "    \n",
      "    Two-by-four array of samples from N(3, 6.25):\n",
      "    \n",
      "    >>> 2.5 * np.random.randn(2, 4) + 3\n",
      "    array([[-4.49401501,  4.00950034, -1.81814867,  7.29718677],  #random\n",
      "           [ 0.39924804,  4.68456316,  4.99394529,  4.84057254]]) #random\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.random.randn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.47997528894691466"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.randn() # 生成一个随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.55488741, 2.85372333, 0.66405357, 4.83210618],\n",
       "       [2.80709148, 4.74493989, 6.85207212, 4.38912051]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Two-by-four array of sample from N(3, 6.25)\n",
    "2.5 * np.random.randn(2, 4) + 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.26548969, -0.09788105, -1.88329091, -0.16135623],\n",
       "       [ 0.6179974 , -1.96764681,  1.15947654, -1.90034503]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randn(2, 4) # 2行4列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.35953941,  2.13314562, -1.63968609,  3.38960317],\n",
       "       [-0.33167701, -4.88764409,  0.62340577, -2.32451255]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2.5 * np.random.randn(2, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## np.random.rand(d0，d1，……，dn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create an array of the given shape and populate it with random samples from a uniform distribution\n",
    "over ``[0, 1)``."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function rand:\n",
      "\n",
      "rand(...) method of mtrand.RandomState instance\n",
      "    rand(d0, d1, ..., dn)\n",
      "    \n",
      "    Random values in a given shape.\n",
      "    \n",
      "    Create an array of the given shape and populate it with\n",
      "    random samples from a uniform distribution\n",
      "    over ``[0, 1)``.\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    d0, d1, ..., dn : int, optional\n",
      "        The dimensions of the returned array, should all be positive.\n",
      "        If no argument is given a single Python float is returned.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : ndarray, shape ``(d0, d1, ..., dn)``\n",
      "        Random values.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    random\n",
      "    \n",
      "    Notes\n",
      "    -----\n",
      "    This is a convenience function. If you want an interface that\n",
      "    takes a shape-tuple as the first argument, refer to\n",
      "    np.random.random_sample .\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.random.rand(3,2)\n",
      "    array([[ 0.14022471,  0.96360618],  #random\n",
      "           [ 0.37601032,  0.25528411],  #random\n",
      "           [ 0.49313049,  0.94909878]]) #random\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.random.rand)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.19567266, 0.56923471],\n",
       "       [0.28427087, 0.76335571],\n",
       "       [0.57129311, 0.77091802]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(3, 2) # 3行2列，随机样本分布在[0,1)区间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## numpy.random.randint(low,high=None,size=None)\n",
    "生成一定范围内的元素为整数的array。  \n",
    "\n",
    "low为下限，size为生成的array大小，如果high为none则生成范围为0-low的均匀分布随机数；  \n",
    "如果指定了high，则生成low-high的均匀分布随机数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 5, 5, 6],\n",
       "       [7, 5, 6, 5]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如生成2*4的在(5,8)的array，则：\n",
    "np.random.randint(5, 8, size=(2,4)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function randint:\n",
      "\n",
      "randint(...) method of mtrand.RandomState instance\n",
      "    randint(low, high=None, size=None, dtype='l')\n",
      "    \n",
      "    Return random integers from `low` (inclusive) to `high` (exclusive).\n",
      "    \n",
      "    Return random integers from the \"discrete uniform\" distribution of\n",
      "    the specified dtype in the \"half-open\" interval [`low`, `high`). If\n",
      "    `high` is None (the default), then results are from [0, `low`).\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    low : int\n",
      "        Lowest (signed) integer to be drawn from the distribution (unless\n",
      "        ``high=None``, in which case this parameter is one above the\n",
      "        *highest* such integer).\n",
      "    high : int, optional\n",
      "        If provided, one above the largest (signed) integer to be drawn\n",
      "        from the distribution (see above for behavior if ``high=None``).\n",
      "    size : int or tuple of ints, optional\n",
      "        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then\n",
      "        ``m * n * k`` samples are drawn.  Default is None, in which case a\n",
      "        single value is returned.\n",
      "    dtype : dtype, optional\n",
      "        Desired dtype of the result. All dtypes are determined by their\n",
      "        name, i.e., 'int64', 'int', etc, so byteorder is not available\n",
      "        and a specific precision may have different C types depending\n",
      "        on the platform. The default value is 'np.int'.\n",
      "    \n",
      "        .. versionadded:: 1.11.0\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : int or ndarray of ints\n",
      "        `size`-shaped array of random integers from the appropriate\n",
      "        distribution, or a single such random int if `size` not provided.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    random.random_integers : similar to `randint`, only for the closed\n",
      "        interval [`low`, `high`], and 1 is the lowest value if `high` is\n",
      "        omitted. In particular, this other one is the one to use to generate\n",
      "        uniformly distributed discrete non-integers.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.random.randint(2, size=10)\n",
      "    array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])\n",
      "    >>> np.random.randint(1, size=10)\n",
      "    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\n",
      "    \n",
      "    Generate a 2 x 4 array of ints between 0 and 4, inclusive:\n",
      "    \n",
      "    >>> np.random.randint(5, size=(2, 4))\n",
      "    array([[4, 0, 2, 1],\n",
      "           [3, 2, 2, 0]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(np.random.randint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 0, 1, 1, 0, 1, 0, 1, 1])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(2, size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(1, size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 4, 4, 4],\n",
       "       [3, 1, 1, 2]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# generate a 2*4 array of ints between 0 and 4, inclusive:\n",
    "np.random.randint(5, size=(2, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
